const {
	connection
} = require('../utils/dbConfig.js')
// const request = require('request')
const appId = "wxc422664b2729853e";
const appSecret = "37133f708e368d4e2db7454a1d3d39a6";

// 测试
test = (req, res) => {
	res.send({
		"code": 1,
		data: [{
				id: 1,
				name: 'html'
			},
			{
				id: 2,
				name: 'cadwdaw2asdass'
			},
			{
				id: 3,
				name: 'javascript'
			},
		]
	})
}

getEmployeeList = (req, res) => {

	connection.query('SELECT * FROM employees', (err, data) => {
		if (err) {
			console.log('[SELECT ERROR] - ', err);
			return;
		}
		console.log('获取员工信息成功：', data);
		// 这里可以将results发送到前端或进行其他处理
		res.send({
			data,
			code: 200
		});
	});
}

addEmployeeInfo = (req, res) => {
	console.log('提交成功111', req.body)
	var addSql =
		'INSERT INTO `employees` (`name`, `id_number`, `gender`, `birth_date`, `photo`, `salary`, `join_date`, `resume`) ' +
		'VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
	var addSqlParams = [
		req.body.name,
		req.body.idNumber,
		req.body.gender,
		req.body.birthDate,
		req.body.photo,
		req.body.salary,
		req.body.joinDate,
		req.body.resume
	];

	connection.query(addSql, addSqlParams, function(err, data) {
		if (err) {
			console.log('[INSERT ERROR] - ', err);
			res.send({
				code: 403
			});
			return
		}
		res.send({
			code: 200
		});
		console.log('提交成功');
	});
}

async function updateEmployeeInfo(req, res) {
	const {
		id
	} = req.params;
	console.log('修改成功111', req.body, req.params)
	const {
		name,
		idNumber,
		gender,
		birthDate,
		photo,
		salary,
		joinDate,
		resume
	} = req.body;

	const updateSql =
		'UPDATE `employees` SET ' +
		'`name` = ?, ' +
		'`id_number` = ?, ' +
		'`gender` = ?, ' +
		'`birth_date` = ?, ' +
		'`photo` = ?, ' +
		'`salary` = ?, ' +
		'`join_date` = ?, ' +
		'`resume` = ? ' +
		'WHERE `id` = ?';

	const updateSqlParams = [
		name,
		idNumber,
		gender,
		birthDate,
		photo,
		salary,
		joinDate,
		resume,
		id
	];

	connection.query(updateSql, updateSqlParams, function(err, data) {
		if (err) {
			console.log('[UPDATE ERROR] - ', err);
			res.send({
				code: 403,
				message: '更新失败'
			});
			return;
		}
		res.send({
			code: 200,
			message: '员工信息更新成功'
		});
		console.log('更新成功');
	});
}

async function deleteEmployeeInfo(req, res) {
	const {
		id
	} = req.params;

	const deleteSql = 'DELETE FROM `employees` WHERE `id` = ?';

	connection.query(deleteSql, [id], function(err, data) {
		if (err) {
			console.log('[DELETE ERROR] - ', err);
			res.send({
				code: 403,
				message: '删除失败'
			});
			return;
		}
		res.send({
			code: 200,
			message: '员工信息删除成功'
		});
		console.log('删除成功');
	});
}

getEmployeeById = (req, res) => {
	const {
		id
	} = req.params; // 从URL参数中获取员工ID

	// 构建SQL查询语句
	const sql = 'SELECT * FROM `employees` WHERE `id` = ?';
	// 执行查询
	connection.query(sql, [id], function(err, data) {
		if (err) {
			console.log('[SELECT ERROR] - ', err);
			return;
		}

		// 发送查询到的员工信息
		console.log(data[0], 'dawdadaw');
		res.send({
			data: data[0],
			code: 200
		});
	});
};

getEmployeeByName = (req, res) => {
	const {
		name
	} = req.params; // 从URL参数中获取姓名
	const sql = 'SELECT * FROM `employees` WHERE `name` LIKE ?';
	const searchName = `%${name}%`; // 使用LIKE语句进行模糊搜索

	connection.query(sql, [searchName], (err, data) => {
		if (err) {
			console.log('[SELECT ERROR] - ', err);
			res.status(500).send({
				code: 500,
				message: '数据库查询错误'
			});
			return;
		}
		// 发送查询到的员工信息
		res.send({
			data,
			code: 200
		});
	});
}

module.exports = {
	test,
	addEmployeeInfo,
	getEmployeeList,
	updateEmployeeInfo,
	deleteEmployeeInfo,
	getEmployeeById,
	getEmployeeByName
}